import { defineStore } from 'pinia'

export const useMsgStore = defineStore({
  id: 'msgInfo',
  state: () => ({
    chatHistory: [],
  }),
  actions: {
    setInfo: function(data) {
      this.chatHistory = data;
    },
    getInfo: function() {
      return this.chatHistory;
    },
    removeInfo: function() {
      this.chatHistory=[];
    },
    removeAndGetP(sendId) {
        // 找到第一个满足条件的项
        const index = this.chatHistory.findIndex(chat => chat.sendId === sendId && chat.chatType === 0);
        if (index !== -1) {
          const result = this.chatHistory[index];
          // 从 chatHistory 中删除该项
          this.chatHistory.splice(index, 1);
          return result;
        }
        return null; // 如果没有找到满足条件的项，则返回 null
      },
      removeAndGetG(groupId) {
        // 找到第一个满足条件的项
        const index = this.chatHistory.findIndex(chat => chat.groupId === groupId);
        if (index !== -1) {
          const result = this.chatHistory[index];
          // 从 chatHistory 中删除该项
          this.chatHistory.splice(index, 1);
          return result;
        }
        return null; // 如果没有找到满足条件的项，则返回 null
      },
    addInfo: function(data) {
      this.chatHistory.push(data);
    },
    getCount: function() {
      return this.chatHistory.length;
  },
    leftAdd:function(data){
      this.chatHistory.unshift(data);
    }
  }
})
